﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace Usermanagment.DBConnection
{
   public class DBUpdate
    {
        private DBConnect dbc;
        private DBDatafetch dbd;
        public DBUpdate(DBConnect dbc)
        {
            this.dbc = dbc;
            dbd = new DBDatafetch(dbc);
        }

        public Boolean updateChips(String username, int chips)
        {
            int id = dbd.getID(username);
            if (id <= 0)
                return false;
            else
            {
                String command = "UPDATE PlayersDB SET Chips =" + chips + " WHERE ID =" + id;
                MySqlCommand cmd = new MySqlCommand(command,dbc.getCon());
                if (cmd.ExecuteNonQuery() > -1)
                    return true;
                else
                    return false;
            }
        }

        public Boolean updateCanplay(String username, int canp)
        {
            int id = dbd.getID(username);
            if (id <= 0)
                return false;
            else
            {
                String command = "UPDATE PlayersDB SET CanPlay ='" +canp  + "' WHERE ID =" + id;
                MySqlCommand cmd = new MySqlCommand(command,dbc.getCon());
                if (cmd.ExecuteNonQuery() > -1)
                    return true;
                else
                    return false;
            }

        }

        public Boolean updateCancon(String username, int canc)
        {
            int id = dbd.getID(username);
            if (id <= 0)
                return false;
            else
            {
                String command = "UPDATE PlayersDB SET Canconnect ='" + canc + "' WHERE ID =" + id;
                MySqlCommand cmd = new MySqlCommand(command, dbc.getCon());
                if (cmd.ExecuteNonQuery() > -1)
                    return true;
                else
                    return false;
            }

        }

		public Boolean updateWins(String username, int wins)
		{
			int t = dbd.getWins(username) + wins;
			String command = "UPDATE Playerstat SET `Wins` = '" + t + "' WHERE `Username` = '" + username + "'";
			MySqlCommand cmd = new MySqlCommand(command, dbc.getCon());
			if (cmd.ExecuteNonQuery() > -1)
				return true;
			else
				return false;
		}

	    public Boolean updateLoss(String username, int loss)
		{

			int t = dbd.getLoss(username) + loss;
			String command = "UPDATE Playerstat SET `Loss` = '" + t + "' WHERE `Username` = '" + username + "'";
			MySqlCommand cmd = new MySqlCommand(command, dbc.getCon());
			if (cmd.ExecuteNonQuery() > -1)
				return true;
			else
				return false;
	    }

	    public Boolean updateRatio(String username)
		{
			double d = dbd.getWins(username) / dbd.getLoss(username);
			String command = "UPDATE Playerstat SET `Ratio` ='" + Convert.ToString(d) + "' WHERE `Username` ='" + username + "'";
			MySqlCommand cmd = new MySqlCommand(command, dbc.getCon());
			if (cmd.ExecuteNonQuery() > -1)
				return true;
			else
				return false;
		}

        public Boolean deleteUser(String username)
        {
            int id = dbd.getID(username);
            if (id <= 0)
                return false;
            else
            {
                String command = "DELETE FROM PlayersDB WHERE ID =" + id;
                MySqlCommand cmd = new MySqlCommand(command, dbc.getCon());
				if (cmd.ExecuteNonQuery() > -1)
				{
					cmd.CommandText = "DELETE FROM Playerstat WHERE `Username` = '" + username + "'";
					cmd.ExecuteNonQuery();
					return true;
				}
				else
					return false;
            }
        }
    }
}
